package com.boot.web.controller;

import java.util.Date;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.Address;
import com.boot.web.service.AddressService;

/**
 * 用户地址控制器
 * @author 【zhouJie】
 * @date 2021-06-02 08:43:22
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/save")
    public ResponseServer save(Address address) {
        address.setIsDefault("0");
        address.setFullAddress(address.getProvince() + address.getCity() + address.getCounty() + address.getAddress());
        address.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", addressService.save(address));
    }

    @PutMapping("/update")
    public ResponseServer update(Address address) {
        address.setFullAddress(address.getProvince() + address.getCity() + address.getCounty() + address.getAddress());
        address.setUpdateTime(new Date());
        return ResponseServer.success("修改成功", addressService.update(address));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", addressService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", addressService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(addressService.getAddressVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(addressService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(addressService.getById(id));
    }

}
